﻿Imports System.Runtime.Serialization

<Serializable()> _
Public Class NailService
    Implements ISerializable

#Region "Properties"

    Private _code As Integer
    Public Property UniqueCode() As Integer
        Get
            Return _code
        End Get
        Set(ByVal value As Integer)
            _code = value
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _price As Double
    Public Property Price() As Double
        Get
            Return _price
        End Get
        Set(ByVal value As Double)
            _price = value
        End Set
    End Property

    Private _valid As Boolean
    Public Property IsValid() As Boolean
        Get
            Return _valid
        End Get
        Set(ByVal value As Boolean)
            _valid = value
        End Set
    End Property

    Private _categoryCode As Integer
    Public Property CategoryCode() As Integer
        Get
            Return _categoryCode
        End Get
        Set(ByVal value As Integer)
            _categoryCode = value
        End Set
    End Property

    Private _dateChanged As Date
    Public Property DateChanged() As Date
        Get
            Return _dateChanged
        End Get
        Set(ByVal value As Date)
            _dateChanged = value
        End Set
    End Property

#End Region

#Region "Creation"

    Protected Sub New()
    End Sub

    Public Sub New(ByVal info As SerializationInfo, _
                   ByVal context As StreamingContext)
        Me.UniqueCode = info.GetInt32("UniqueCode")
        Me.Name = info.GetString("Name")
        Me.Price = info.GetDouble("Price")
        Me.IsValid = info.GetBoolean("IsValid")
        Me.CategoryCode = info.GetInt32("CategoryCode")
        Me.DateChanged = info.GetDateTime("DateChanged")
    End Sub

    Public Sub GetObjectData(ByVal info As SerializationInfo, _
                             ByVal context As StreamingContext) _
                             Implements ISerializable.GetObjectData
        info.AddValue("UniqueCode", Me.UniqueCode)
        info.AddValue("Name", Me.Name)
        info.AddValue("Price", Me.Price)
        info.AddValue("IsValid", Me.IsValid)
        info.AddValue("CategoryCode", Me.CategoryCode)
        info.AddValue("DateChanged", Me.DateChanged)
    End Sub

    Public Shared Function CreateNewNailService() As NailService
        Return New NailService()
    End Function

    ''' <summary>
    ''' Creates specified nail service .
    ''' </summary>
    ''' <param name="code"></param>
    ''' <param name="name"></param>
    ''' <param name="price"></param>
    ''' <param name="valid"></param>
    ''' <returns><see cref="NailService">New nail service</see></returns>
    ''' <remarks>Date changed is today</remarks>
    Public Shared Function CreateNailService(ByVal code As Integer, _
                                             ByVal name As String, _
                                             ByVal price As Double, _
                                             ByVal valid As Boolean, _
                                             ByVal category As Integer) As NailService
        Dim n = New NailService()
        With n
            .UniqueCode = code
            .Name = name
            .Price = price
            .IsValid = valid
            .CategoryCode = category
            .DateChanged = Date.Today
        End With

        Return n
    End Function
#End Region

#Region "Overriden Object"

    Public Overrides Function GetHashCode() As Integer
        Dim hashCode As Integer
        hashCode += UniqueCode.GetHashCode()
        hashCode += Name.GetHashCode()
        hashCode += Price.GetHashCode()
        hashCode += IsValid.GetHashCode()
        hashCode += CategoryCode.GetHashCode()
        hashCode += DateChanged.GetHashCode()

        Return hashCode
    End Function

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Dim other As NailService = TryCast(obj, NailService)
        If other Is Nothing Then
            Return False
        End If

        Return UniqueCode = other.UniqueCode
    End Function

#End Region

End Class
